home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / TWOID.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.6 KB  |  330 lines

  1. /* 
  2.  * twistoid.c
  3.  *
  4.  * FUNCTION:
  5.  * Show extrusion of open contours. Also, show how torsion is applied.
  6.  *
  7.  * HISTORY:
  8.  * -- linas Vepstas October 1991
  9.  * -- heavily modified to draw corrugated surface, Feb 1993, Linas
  10.  * -- modified to demo twistoid March 1993
  11.  * -- port to glut Linas Vepstas March 1995
  12.  */
  13.  
  14. /* required include files */
  15. #include <math.h>
  16. #include <stdlib.h>
  17. #include <GL/glut.h>
  18. #include <GL/tube.h>
  19.  
  20. /* Some <math.h> files do not define M_PI... */
  21. #ifndef M_PI
  22. #define M_PI 3.14159265358979323846
  23. #endif
  24.  
  25. static int mx = 121;
  26. static int my = 121;
  27.  
  28. #define OPENGL_10
  29. /* =========================================================== */
  30.  
  31. #define NUM_TOID1_PTS 5
  32. double toid1_points[NUM_TOID1_PTS][3];
  33. float toid1_colors [NUM_TOID1_PTS][3];
  34. double toid1_twists [NUM_TOID1_PTS];
  35.  
  36. #define TSCALE (6.0)
  37.  
  38. #define TPTS(x,y) {                \
  39.    toid1_points[i][0] = TSCALE * (x);        \
  40.    toid1_points[i][1] = TSCALE * (y);        \
  41.    toid1_points[i][2] = TSCALE * (0.0);        \
  42.    i++;                        \
  43. }
  44.  
  45. #define TCOLS(r,g,b) {                \
  46.    toid1_colors[i][0] = (r);            \
  47.    toid1_colors[i][1] = (g);            \
  48.    toid1_colors[i][2] = (b);            \
  49.    i++;                        \
  50. }
  51.  
  52. #define TXZERO() {                \
  53.    toid1_twists[i] = 0.0;            \
  54.    i++;                        \
  55. }
  56.  
  57. void init_toid1_line (void)
  58. {
  59.    int i;
  60.  
  61.    i=0;
  62.    TPTS (-1.1, 0.0);
  63.    TPTS (-1.0, 0.0);
  64.    TPTS (0.0, 0.0);
  65.    TPTS (1.0, 0.0);
  66.    TPTS (1.1, 0.0);
  67.  
  68.    i=0;
  69.    TCOLS (0.8, 0.8, 0.5);
  70.    TCOLS (0.8, 0.4, 0.5);
  71.    TCOLS (0.8, 0.8, 0.3);
  72.    TCOLS (0.4, 0.4, 0.5);
  73.    TCOLS (0.8, 0.8, 0.5);
  74.  
  75.    i=0;
  76.    TXZERO ();
  77.    TXZERO ();
  78.    TXZERO ();
  79.    TXZERO ();
  80.    TXZERO ();
  81. }
  82.  
  83. /* =========================================================== */
  84.  
  85. #define SCALE 0.6
  86. #define TWIST(x,y) {                        \
  87.    double ax, ay, alen;                        \
  88.    twistation[i][0] = SCALE * (x);                \
  89.    twistation[i][1] = SCALE * (y);                \
  90.    if (i!=0) {                            \
  91.       ax = twistation[i][0] - twistation[i-1][0];        \
  92.       ay = twistation[i][1] - twistation[i-1][1];        \
  93.       alen = 1.0 / sqrt (ax*ax + ay*ay);            \
  94.       ax *= alen;   ay *= alen;                    \
  95.       twist_normal [i-1][0] = - ay;                \
  96.       twist_normal [i-1][1] = ax;                \
  97.    }                                \
  98.    i++;                                \
  99. }
  100.  
  101. #define NUM_TWIS_PTS (20)
  102.  
  103. double twistation [NUM_TWIS_PTS][2];
  104. double twist_normal [NUM_TWIS_PTS][2];
  105.  
  106. void init_tripples (void)
  107. {
  108.    int i;
  109.    double angle;
  110.    double co, si;
  111.  
  112.    /* outline of extrusion */
  113.    i=0;
  114.    /* first, draw a semi-curcular "hump" */
  115.    while (i< 11) {
  116.       angle = M_PI * ((double) i) / 10.0;
  117.       co = cos (angle);
  118.       si = sin (angle);
  119.       TWIST ((-7.0 -3.0*co), 1.8*si);
  120.    }
  121.  
  122.    /* now, a zig-zag corrugation */
  123.    while (i< NUM_TWIS_PTS) {
  124.       TWIST ((-10.0 +(double) i), 0.0);
  125.       TWIST ((-9.5 +(double) i), 1.0);
  126.    }
  127. }
  128.  
  129.    
  130. /* =========================================================== */
  131.  
  132. #define V3F(x,y,z) {                    \
  133.     float vvv[3];                     \
  134.     vvv[0] = x; vvv[1] = y; vvv[2] = z; v3f (vvv);     \
  135. }
  136.  
  137. #define N3F(x,y,z) {                    \
  138.     float nnn[3];                     \
  139.     nnn[0] = x; nnn[1] = y; nnn[2] = z; n3f (nnn);     \
  140. }
  141.  
  142. /* =========================================================== */
  143.  
  144. void draw_twist (void) {
  145.    int i;
  146.  
  147.    toid1_twists[2] = (double) (mx-121) / 8.0;
  148.  
  149.    i=3;
  150. /*
  151.    TPTS (1.0, ((double)my) /400.0);
  152.    TPTS (1.1, 1.1 * ((double)my) / 400.0);
  153. */
  154.    TPTS (1.0, -((double)(my-121)) /200.0);
  155.    TPTS (1.1, -1.1 * ((double)(my-121)) / 200.0);
  156.  
  157. #ifdef IBM_GL_32
  158.    rotate (230, 'x');
  159.    rotate (230, 'y');
  160.    scale (1.8, 1.8, 1.8);
  161.  
  162.    if (mono_color) {
  163.       RGBcolor (178, 178, 204);
  164.       twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
  165.                 NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
  166.    } else {
  167.       twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
  168.               NULL, NUM_TOID1_PTS, toid1_points, toid1_colors, toid1_twists);
  169.    }
  170. #endif
  171.  
  172. #ifdef OPENGL_10
  173.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  174.  
  175.    /* set up some matrices so that the object spins with the mouse */
  176.    glPushMatrix ();
  177.    glTranslatef (0.0, 0.0, -80.0);
  178.    glRotated (43.0, 1.0, 0.0, 0.0);
  179.    glRotated (43.0, 0.0, 1.0, 0.0);
  180.    glScaled (1.8, 1.8, 1.8);
  181.    gleTwistExtrusion (NUM_TWIS_PTS, twistation, twist_normal, 
  182.               NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
  183.    glPopMatrix ();
  184.    glutSwapBuffers ();
  185. #endif
  186.  
  187. }
  188.  
  189. /* =========================================================== */
  190.  
  191. void init_twist (void)
  192. {
  193.    int js;
  194.  
  195.    init_toid1_line ();
  196.    init_tripples ();
  197.  
  198. #ifdef IBM_GL_32
  199.    js = getjoinstyle ();
  200.    js &= ~TUBE_CONTOUR_CLOSED;
  201.    setjoinstyle (js);
  202. #endif
  203.  
  204. #ifdef OPENGL_10
  205.    js = gleGetJoinStyle ();
  206.    js &= ~TUBE_CONTOUR_CLOSED;
  207.    gleSetJoinStyle (js);
  208. #endif
  209.  
  210. }
  211.  
  212. /* get notified of mouse motions */
  213. void MouseMotion (int x, int y)
  214. {
  215.    mx = x;
  216.    my = y;
  217.    glutPostRedisplay ();
  218. }
  219.  
  220. void JoinStyle (int msg) 
  221. {
  222.    int style;
  223.    /* get the current joint style */
  224.    style = gleGetJoinStyle ();
  225.  
  226.    /* there are four different join styles, 
  227.     * and two different normal vector styles */
  228.    switch (msg) {
  229.       case 0:
  230.          style &= ~TUBE_JN_MASK;
  231.          style |= TUBE_JN_RAW;
  232.          break;
  233.       case 1:
  234.          style &= ~TUBE_JN_MASK;
  235.          style |= TUBE_JN_ANGLE;
  236.          break;
  237.       case 2:
  238.          style &= ~TUBE_JN_MASK;
  239.          style |= TUBE_JN_CUT;
  240.          break;
  241.       case 3:
  242.          style &= ~TUBE_JN_MASK;
  243.          style |= TUBE_JN_ROUND;
  244.          break;
  245.  
  246.       case 20:
  247.          style &= ~TUBE_NORM_MASK;
  248.          style |= TUBE_NORM_FACET;
  249.          break;
  250.       case 21:
  251.          style &= ~TUBE_NORM_MASK;
  252.          style |= TUBE_NORM_EDGE;
  253.          break;
  254.  
  255.       case 99:
  256.          exit (0);
  257.  
  258.       default:
  259.          break;
  260.    }
  261.    gleSetJoinStyle (style);
  262.    glutPostRedisplay ();
  263. }
  264.  
  265. /* set up a light */
  266. GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
  267. GLfloat lightOneColor[] = {0.89, 0.89, 0.89, 1.0}; 
  268.  
  269. GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
  270. GLfloat lightTwoColor[] = {0.89, 0.89, 0.89, 1.0}; 
  271.  
  272. GLfloat material[] = {0.93, 0.79, 0.93, 1.0}; 
  273.  
  274. int
  275. main (int argc, char * argv[]) {
  276.  
  277.    /* initialize glut */
  278.    glutInit (&argc, argv);
  279.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  280.    glutCreateWindow ("twistoid");
  281.    glutDisplayFunc (draw_twist);
  282.    glutMotionFunc (MouseMotion);
  283.  
  284.    /* create popup menu */
  285.    glutCreateMenu (JoinStyle);
  286.    glutAddMenuEntry ("Raw Join Style", 0);
  287.    glutAddMenuEntry ("Angle Join Style", 1);
  288.    glutAddMenuEntry ("Cut Join Style", 2);
  289.    glutAddMenuEntry ("Round Join Style", 3);
  290.    glutAddMenuEntry ("------------------", 9999);
  291.    glutAddMenuEntry ("Facet Normal Vectors", 20);
  292.    glutAddMenuEntry ("Edge Normal Vectors", 21);
  293.    glutAddMenuEntry ("------------------", 9999);
  294.    glutAddMenuEntry ("Exit", 99);
  295.    glutAttachMenu (GLUT_MIDDLE_BUTTON);
  296.  
  297.    /* initialize GL */
  298.    glClearDepth (1.0);
  299.    glEnable (GL_DEPTH_TEST);
  300.    glClearColor (0.2, 0.2, 0.2, 0.0);
  301.    glShadeModel (GL_SMOOTH);
  302.  
  303.    glMatrixMode (GL_PROJECTION);
  304.    /* roughly, measured in centimeters */
  305.    glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0);
  306.    glMatrixMode(GL_MODELVIEW);
  307.  
  308.    /* initialize lighting */
  309.    glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, material);
  310.    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, material);
  311.    glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
  312.    glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
  313.    glLightfv (GL_LIGHT0, GL_SPECULAR, lightOneColor);
  314.    glEnable (GL_LIGHT0);
  315.    glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
  316.    glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
  317.    glEnable (GL_LIGHT1);
  318.    glEnable (GL_LIGHTING);
  319. /*
  320.    glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
  321.    glEnable (GL_COLOR_MATERIAL);
  322. */
  323.  
  324.    init_twist ();
  325.  
  326.    glutMainLoop ();
  327.    return 0;             /* ANSI C requires main to return int. */
  328. }
  329. /* ------------------ end of file -------------------- */
  330.